<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column prop="id" label="ID" width="70"></el-table-column>
      <el-table-column prop="name" label="模块名称" width="150"></el-table-column>
      <el-table-column prop="type" label="模块类别" width="120"></el-table-column>
      <el-table-column prop="url" label="模块 URL" width="220"></el-table-column>
      <el-table-column prop="seatNames" label="绑定席位" width="200">
        <template slot-scope="scope">
          <el-tag size="mini" v-for="r in scope.row.seatNames" :key="r">{{r}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述" width="300"></el-table-column>
      <el-table-column prop="createdTime" label="创建时间" width="180"></el-table-column>
      <el-table-column prop="updatedTime" label="更新时间" width="180"></el-table-column>
      <el-table-column fixed="right" label="操作" width="160">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row.id)" type="text" size="small">编辑</el-button>
          <el-button @click="handleGetSeatList(scope.row.id)" type="text" size="small">绑定席位</el-button>
          <el-button @click="handleDelete(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="编辑" :visible.sync="dialogVisible">
      <el-form label-width="80px">
        <el-form-item label="ID" hidden>
          <el-input v-model="pojo.id" placeholder="id" />
        </el-form-item>
        <el-form-item label="模块名称">
          <el-input v-model="pojo.name" placeholder="名称" />
        </el-form-item>
        <el-form-item label="模块类型">
          <el-input v-model="pojo.type" placeholder="类型" />
        </el-form-item>
        <el-form-item label="模块URL">
          <el-input v-model="pojo.url" placeholder="模块URL" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="pojo.description" placeholder="描述" />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleUpdate(pojo.id)" type="primary">保存</el-button>
          <el-button @click="dialogVisible=false">关闭</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog title="分配席位" :visible.sync="dialogVisible2">
      <el-checkbox-group v-model="checkedSeats">
        <el-checkbox v-for="r in seats" :label="r.id" :key="r.id" border>{{r.name}}</el-checkbox>
      </el-checkbox-group>

      <div style="margin-top: 20px">
        <el-button @click="handleFpSeat()" type="primary">保存</el-button>
        <el-button @click="dialogVisible2=false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList,findById,update,deleteById,bdSeats } from '@/api/module'
import { getList as getSeatList } from '@/api/seat'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      param: { },
      listLoading: true,
      dialogVisible: false, // 编辑窗口是否可见
      pojo: {},             // 编辑表单绑定的实体对象
      dialogVisible2: false,
      seats: [],
      currentId: '',
      checkedSeats: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList().then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    handleEdit(id) {
      this.dialogVisible=true
      findById(id).then(response => {
        if(response.code === 20000){
          this.pojo=response.data
        }
      })
    },
    handleDelete(id) {
      this.$confirm('确定要删除此纪录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById(id).then(response => {
          if (response.code === 20000) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.fetchData()
          } else {
            this.$message({
              message: '删除失败',
              type: 'error'
            })
          }
        })
      })
    },
    handleUpdate(id) {
      this.dialogVisible = true
      update(id,this.pojo).then(response => {
        if(response.code === 20000) {
          this.$message({
            message: '更新成功',
            type: 'success'
          })
          this.dialogVisible = false
          this.fetchData()
        } else {
          this.$message({
            message: '更新失败',
            type: 'error'
          })
        }
      })
    },
    handleGetSeatList(id) {
      this.dialogVisible2 = true;
      this.currentId = id;
      this.checkedSeats = [];
      getSeatList().then(response=>{
        if(response.code === 20000){
          this.seats = response.data
        }
      })
    },
    handleFpSeat(){
      bdSeats(this.currentId, this.checkedSeats).then(response => {
        if(response.code === 20000){
          this.$message({
            message: '更新成功',
            type: 'success'
          })
          this.dialogVisible2 = false
          this.fetchData()
        } else {
          this.$message({
            message: '更新失败',
            type: 'error'
          })
        }
      })
    },
  }
}
</script>
